package com.wise.site.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;

import com.wise.site.WiseSiteApplication;

import android.os.AsyncTask;

/**
 * 缓存数据到本地文件
 * 
 * @author huangyue
 * 
 *         2012-10-19
 */
public class FileCache  {
	private static final String NAME = "cache.dc";
	private File cacheDir;
	private static FileCache mCache;
	
	private HashMap<String, Object> mData;
	private Thread quee;
	private Vector<Task> tasks = new Vector<FileCache.Task>();

	private FileCache() {
		// Find the dir to save cached images
		if (android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED)) {
			cacheDir = new File(Util.getExternDir("/wise/cache/"));
		} else
			cacheDir = WiseSiteApplication.getContext().getCacheDir();
		if (!cacheDir.exists())
			cacheDir.mkdirs();
		
		initCache();
		if(mData == null){
			mData = new HashMap<String, Object>();
		}
	}

	public static FileCache getInstance() {
		if (mCache == null) {
			mCache = new FileCache();
		}

		return mCache;
	}

	public synchronized void put(String key, Object value, boolean save) {
		mData.put(key, value);
		if (save) {
			saveCache();
		}
	}

	public void initCache() {
		File f = new File(cacheDir, NAME);
		if(mData != null){
			return;
		}
		if (!f.exists()) {
			mData = new HashMap<String, Object>();
			return;
		}
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(f);
			ObjectInputStream ois = new ObjectInputStream(fis);
			mData = (HashMap<String, Object>) ois.readObject();
			ois.close();
		} catch (Exception e) {
			System.out.println(e);
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public Object get(String key){
		return mData.get(key);
	}

	public void saveCache() {
		tasks.add(new Task());
		
		if(quee == null){
			quee = new Thread(new Runnable() {
				public void run() {
					Task t = null;
					try{
						while (true) {
							t = tasks.remove(0);
							t.run();
						}
					}catch (Exception e) {
					}
					quee = null;
				}
			});
			quee.start();
		}
	}
	
	class Task{
		public void run(){
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(new File(cacheDir, NAME));
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				oos.writeObject(mData);
				oos.flush();
				oos.close();
			} catch (Exception e) {
				Log.w(NAME, "", e);
			} finally {
				if (fos != null) {
					try {
						fos.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}

}
